package main

import (
    "fmt"
    "github.com/micro/go-micro"
    "golang.org/x/net/context"
    "math/rand"
    proto "hello/proto/user"
)

func main() {
    // Create a new service. Optionally include some options here.
    service := micro.NewService(micro.Name("go.micro.cli.user"))
    service.Init()

    // Create new user client
    userService := proto.NewUserService("go.micro.srv.user", service.Client())

    id := randStr(16)
    name := "学院君"
    email := "yaojinbu@163.com"
    password := "test123"

    fmt.Println(id, name, email, password)

    // Call the greeter
    rsp, err := userService.Create(context.TODO(), &proto.User{
        Id: id,
        Name: name,
        Email: email,
        Password: password,
    })
    if err != nil {
        fmt.Errorf("User Register Failed: %v\n", err);
    }
    fmt.Println("User Registered:", rsp.User.Id)

    resp, err := userService.Get(context.TODO(), &proto.User{
        Id: rsp.User.Id,
    })
    if err != nil {
        fmt.Errorf("Could not get user: %v\n", err)
    }
    fmt.Println("User Info:", resp.User)
}

func randStr(n int) string {
    var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
    b := make([]rune, n)
    for i := range b {
        b[i] = letters[rand.Intn(len(letters))]
    }
    return string(b)
}